/*
 * @Author: your name
 * @Date: 2020-08-14 16:10:52
 * @LastEditTime: 2020-08-23 19:13:01
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \ryfront\src\api\http.js
 */
import axios from 'axios';
import router from '@/router'

//设置基本路径
axios.defaults.baseURL = "http://localhost:8081";

//允许携带 cookie 
axios.defaults.withCredentials = true;

//请求头带上token Bearer方式  请百度
axios.defaults.headers.common['Authorization'] =  "Bearer " +localStorage.getItem("token");

//添加请求拦截
axios.interceptors.request.use(function(config){
    //do something before request is sent 
    
    console.log(config);

    return config;
},function (error) {
    console.log("333");
    //do something with request error
    return Promise.reject(error);
});

//添加响应拦截 
axios.interceptors.response.use(function (response) {
    console.log("111");
    return response;
},function (error) {
    if(error.response.status == 801){
        //前端直接跳转到登录页面
        router.push("/login");
    }
    return Promise.reject(error);

})

export default axios;


